import { Router } from 'express';
import { authenticate, authorize } from '../../middleware/auth';
import { LogController } from '../../admin/controllers/logController';
import { logOperation, skipLog } from '../../middleware/operationLog';
import { ALL_PERMISSIONS } from '../../constants/permissions';

const router = Router();
const logController = new LogController();
/**
 * @swagger
 * /api/logs/clear:
 *   delete:
 *     summary: 清空操作日志
 *     tags: [日志管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: days
 *         schema:
 *           type: integer
 *           default: 30
 *         description: 保留天数
 *     responses:
 *       200:
 *         description: 清空成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 清空成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     deletedCount:
 *                       type: integer
 *                       description: 删除的记录数
 */
router.delete('/clear', 
  authenticate, 
  authorize([ALL_PERMISSIONS.LOG_DELETE]),
  logOperation('清除日志', '日志管理'),
  logController.clearOperationLogs
);

/**
 * @swagger
 * /api/logs/stats:
 *   get:
 *     summary: 获取日志统计信息
 *     tags: [日志管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: period
 *         schema:
 *           type: string
 *           enum: [today, week, month, year]
 *           default: week
 *         description: 统计周期
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 查询成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     totalLogs:
 *                       type: integer
 *                       description: 总日志数
 *                     todayLogs:
 *                       type: integer
 *                       description: 今日日志数
 *                     userStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           userId:
 *                             type: integer
 *                           username:
 *                             type: string
 *                           realName:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     actionStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           action:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     resourceStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           resource:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     hourlyStats:
 *                       type: object
 *                       description: 小时统计
 *                     dailyStats:
 *                       type: object
 *                       description: 每日统计
 */
router.get('/stats', authenticate, authorize([ALL_PERMISSIONS.LOG_STATS_READ]), skipLog, logController.getLogStats);

/**
 * @swagger
 * /api/logs/analytics:
 *   get:
 *     summary: 获取日志分析数据
 *     tags: [日志管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [action, resource, user, time]
 *           default: action
 *         description: 分析类型
 *       - in: query
 *         name: period
 *         schema:
 *           type: string
 *           enum: [day, week, month]
 *           default: week
 *         description: 分析周期
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 返回数量限制
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 查询成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     summary:
 *                       type: object
 *                       properties:
 *                         totalLogs:
 *                           type: integer
 *                         uniqueUsers:
 *                           type: integer
 *                         uniqueActions:
 *                           type: integer
 *                         uniqueResources:
 *                           type: integer
 *                         errorLogs:
 *                           type: integer
 *                         successLogs:
 *                           type: integer
 *                         successRate:
 *                           type: string
 *                     topUsers:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           userId:
 *                             type: integer
 *                           username:
 *                             type: string
 *                           realName:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     topActions:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           action:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     topResources:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           resource:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     recentActivity:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/OperationLog'
 */
router.get('/analytics', authenticate, authorize([ALL_PERMISSIONS.LOG_READ]), skipLog, logController.getLogAnalytics);

/**
 * @swagger
 * /api/logs/export:
 *   get:
 *     summary: 导出操作日志
 *     tags: [日志管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: format
 *         schema:
 *           type: string
 *           enum: [excel, csv, json]
 *           default: excel
 *         description: 导出格式
 *       - in: query
 *         name: startDate
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期
 *       - in: query
 *         name: endDate
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期
 *       - in: query
 *         name: action
 *         schema:
 *           type: string
 *         description: 操作类型
 *       - in: query
 *         name: resource
 *         schema:
 *           type: string
 *         description: 资源类型
 *       - in: query
 *         name: userId
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 导出成功
 *         content:
 *           application/vnd.openxmlformats-officedocument.spreadsheetml.sheet:
 *             schema:
 *               type: string
 *               format: binary
 *           text/csv:
 *             schema:
 *               type: string
 *           application/json:
 *             schema:
 *               type: object
 */
// router.get('/export', authenticate, authorize([ALL_PERMISSIONS.LOG_EXPORT]),logController.exportOperationLogs);
router.get('/export',logController.exportOperationLogs);

/**
 * @swagger
 * /api/logs/{id}:
 *   get:
 *     summary: 根据ID获取操作日志
 *     tags: [日志管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 日志ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 查询成功
 *                 data:
 *                   $ref: '#/components/schemas/OperationLog'
 */
router.get('/:id', authenticate, authorize([ALL_PERMISSIONS.LOG_READ]), skipLog, logController.getOperationLogById);


/**
 * @swagger
 * components:
 *   schemas:
 *     OperationLog:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 日志ID
 *         action:
 *           type: string
 *           description: 操作类型
 *         resource:
 *           type: string
 *           description: 资源类型
 *         resourceId:
 *           type: string
 *           description: 资源ID
 *         details:
 *           type: string
 *           description: 操作详情
 *         ipAddress:
 *           type: string
 *           description: IP地址
 *         userAgent:
 *           type: string
 *           description: 用户代理
 *         createdAt:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         user:
 *           type: object
 *           properties:
 *             id:
 *               type: integer
 *               description: 用户ID
 *             username:
 *               type: string
 *               description: 用户名
 *             realName:
 *               type: string
 *               description: 真实姓名
 */

/**
 * @swagger
 * /api/logs:
 *   get:
 *     summary: 获取操作日志列表
 *     tags: [日志管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 20
 *         description: 每页数量
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *       - in: query
 *         name: action
 *         schema:
 *           type: string
 *         description: 操作类型
 *       - in: query
 *         name: resource
 *         schema:
 *           type: string
 *         description: 资源类型
 *       - in: query
 *         name: userId
 *         schema:
 *           type: integer
 *         description: 用户ID
 *       - in: query
 *         name: startDate
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期
 *       - in: query
 *         name: endDate
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 查询成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     data:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/OperationLog'
 *                     pagination:
 *                       type: object
 *                       properties:
 *                         page:
 *                           type: integer
 *                           description: 当前页码
 *                         limit:
 *                           type: integer
 *                           description: 每页数量
 *                         total:
 *                           type: integer
 *                           description: 总记录数
 *                         totalPages:
 *                           type: integer
 *                           description: 总页数
 *                         hasNext:
 *                           type: boolean
 *                           description: 是否有下一页
 *                         hasPrev:
 *                           type: boolean
 *                           description: 是否有上一页
 */
router.get('/', authenticate, authorize([ALL_PERMISSIONS.LOG_READ]), skipLog, logController.getOperationLogs);

export default router; 